package edu.mtu.citizenscience.cs.sdk.uploader.json;

import java.util.Calendar;
import java.util.Date;

import android.database.Cursor;
import edu.mtu.citizenscience.cs.sdk.uploader.model.Entry;
import edu.mtu.citizenscience.cs.sdk.uploader.model.EntryType;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * A column in the apps database
 */
public class Column {
	// the name of the column
	private String name;
	// the type of entry stored in the column
	private EntryType type;
	
	/**
	 * Get the column name
	 * 
	 * @param   void
	 * @return	String - the column name
	 */
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * Set the column name
	 * 
	 * @param   name - String - the column name
	 * @return	void
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * Get the column type
	 * 
	 * @param   void
	 * @return	EntryType - the column type
	 */
	public EntryType getType()
	{
		return this.type;
	}
	
	/**
	 * Set the column type
	 * 
	 * @param   type - EntryType - the column type
	 * @return	void
	 */
	public void setType(EntryType type)
	{
		this.type = type;
	}
	
	/**
	 * Get the column entry for a row cursor
	 * 
	 * @param   cursor - Cursor - the row cursor
	 * @return	Entry<?> - the column entry
	 */
	public Entry<?> getEntry(Cursor cursor)
	{
		// get the index of the column in the db
		int index = cursor.getColumnIndex(this.name);
		
		// switch over the possible entry types
		switch (type) 
		{
			case TYPE_BOOLEAN:
				// check if the int value store is 1 or greater
				if (cursor.getInt(index) > 0)
				{
					// create a new boolean entry true
					return new Entry<Boolean>(this.name, this.type, true);
				}
				else
				{
					// create a new boolean entry false
					return new Entry<Boolean>(this.name, this.type, false);
				}
			case TYPE_DATE:
				// get a calendar instance
				Calendar calendar = Calendar.getInstance();
				// set the calendar time to the stored long
		        calendar.setTimeInMillis(cursor.getLong(index));
		        
		        // return a new entry with a date-time from the calendar
				return new Entry<Date>(this.name, this.type, calendar.getTime());
			case TYPE_DOUBLE:
				// return the entry as a double from the database
				return new Entry<Double>(this.name, this.type, cursor.getDouble(index));
			case TYPE_INTEGER:
				// return the entry as a long from the database
				return new Entry<Long>(this.name, this.type, cursor.getLong(index));
			case TYPE_LATITUDE:
				// return the entry as a double from the database
				return new Entry<Double>(this.name, this.type, cursor.getDouble(index));
			case TYPE_LONGITUDE:
				// return the entry as a double from the database
				return new Entry<Double>(this.name, this.type, cursor.getDouble(index));
			case TYPE_STRING:
				// return the entry as a String from the database
				return new Entry<String>(this.name, this.type, cursor.getString(index));
			default:
				return null;
		}
		
	}
	
}
